package com.nineclock.sign.service.impl;

import java.math.BigDecimal;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.metadata.style.WriteFont;
import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.nineclock.common.entity.PageResult;
import com.nineclock.common.enums.ResponseEnum;
import com.nineclock.common.exception.NcException;
import com.nineclock.common.filter.CurrentUserHolder;
import com.nineclock.common.utils.BeanHelper;
import com.nineclock.common.utils.DateTimeUtil;
import com.nineclock.sign.dto.*;
import com.nineclock.sign.excel.CustomHandler;
import com.nineclock.sign.mapper.SignInfoCountMapper;
import com.nineclock.sign.mapper.SignInfoMapper;
import com.nineclock.sign.mapper.SignPictureMapper;
import com.nineclock.sign.pojo.SignInfo;
import com.nineclock.sign.pojo.SignInfoCount;
import com.nineclock.sign.pojo.SignPicture;
import com.nineclock.sign.service.SignInfoService;
import com.nineclock.system.dto.SysCompanyUserDTO;
import com.nineclock.system.dto.SysDepartmentDTO;
import com.nineclock.system.feign.SysCompanyUserFeign;
import lombok.SneakyThrows;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Duration;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author cjh
 * @date 2021/12/2 15:46
 */
@Service
@Transactional
public class SignInfoServiceImpl implements SignInfoService {
    @Autowired
    private SignInfoMapper signInfomapper;

    @Autowired
    private SignPictureMapper signPictureMapper;

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @Autowired
    private SysCompanyUserFeign sysCompanyUserFeign;

    @Autowired
    private SignInfoCountMapper signInfoCountMapper;


    /**
     * APP: 签到
     *
     * @param signInfoDTO 签到信息DTO
     */
    @Override
    public void insert(SignInfoDTO signInfoDTO) throws UnsupportedEncodingException {
        //校验数据
        if (signInfoDTO == null) {
            throw new NcException(ResponseEnum.INVALID_PARAM_ERROR);
        }


        //转换数据
        SignInfo signInfo = BeanHelper.copyProperties(signInfoDTO, SignInfo.class);
        //组装数据 前端没有传的数据
        signInfo.setCompanyId(CurrentUserHolder.get().getCompanyId());
        signInfo.setCompanyUserId(CurrentUserHolder.get().getCompanyUserId());
        signInfo.setCreateTime(new Date());
        signInfo.setCreateDate(DateTimeUtil.dateToStr(new Date(), DateTimeUtil.TIME_FORMAT_2));
        signInfo.setCreateMonth(DateTimeUtil.dateToStr(new Date(), DateTimeUtil.TIME_FORMAT_4));
        signInfomapper.insert(signInfo);

        //获取图片url列表,如果传了图片就添加
        if (!StringUtils.isEmpty(signInfoDTO.getImages())) {
            //添加图片表
            String[] imgList = signInfoDTO.getImages().split(",");
            for (String img : imgList) {
                SignPicture signPic = new SignPicture();
                signPic.setSignId(signInfo.getId());
                signPic.setUrl(img);
                signPictureMapper.insert(signPic);
            }
        }
        //拼接key
        Date now = new Date();
        String nowDateStr = DateTimeUtil.dateToStr(now, DateTimeUtil.TIME_FORMAT_2);
        String nowMonthKey = DateTimeUtil.dateToStr(now, DateTimeUtil.TIME_FORMAT_4);

        String dayKey = "signDay_" + CurrentUserHolder.get().getCompanyUserId() + "_" + nowDateStr;
        String monthKey = "signMonth_" + CurrentUserHolder.get().getCompanyUserId() + "_" + nowMonthKey;
        //把当日签到次数写入redis
        if (redisTemplate.hasKey(dayKey)) {
            redisTemplate.boundValueOps(dayKey).increment(+1);
        } else {
            redisTemplate.opsForValue().set(dayKey, "1", Duration.ofDays(365));
        }

        //把当日签到次数写入redis
        if (redisTemplate.hasKey(monthKey)) {
            redisTemplate.boundValueOps(monthKey).increment(+1);
        } else {
            redisTemplate.opsForValue().set(monthKey, "1", Duration.ofDays(365));
        }

    }

    @Override
    public Integer getDayCount() {
        //拼接key
        Date now = new Date();
        String nowDateStr = DateTimeUtil.dateToStr(now, DateTimeUtil.TIME_FORMAT_2);
        String dayKey = "signDay_" + CurrentUserHolder.get().getCompanyUserId() + "_" + nowDateStr;
        //从redis中取出
        String value = redisTemplate.opsForValue().get(dayKey);
        if (value == null) {
            return 0;
        }

        return Integer.valueOf(value);

    }

    /**
     * 王津代码
     *
     * @param dateStr
     * @return
     */
    @Override
    public List<SignInfoListDTO> listSignAllUser(String dateStr) {


        List<SignInfoListDTO> signResponseListDTOList = new ArrayList<>();

        //查询当前企业员工
        List<SysCompanyUserDTO> companyUserDTOList = sysCompanyUserFeign.queryAllCompanyUser().getData();

        if (!CollectionUtils.isEmpty(companyUserDTOList)) {
            //遍历每一个员工
            for (SysCompanyUserDTO companyUserDTO : companyUserDTOList) {
                //一个员工的签到详情列表
                SignInfoListDTO responseListDTO = new SignInfoListDTO();
                //一个员工的当天签到列表集合
                List<SignInfoDTO> signInfoDTOList = new ArrayList<>();

                //根据员工id查询当前日期内是否签到
                LambdaQueryWrapper<SignInfo> wrapper = new LambdaQueryWrapper<>();
                wrapper.eq(SignInfo::getCompanyUserId, companyUserDTO.getId());
                wrapper.eq(SignInfo::getCreateDate, dateStr);
                List<SignInfo> signInfos = signInfomapper.selectList(wrapper);
                //获取到一个签到集合
                for (SignInfo signInfo : signInfos) {
                    SignInfoDTO signInfoDTO = new SignInfoDTO();
                    //遍历集合封装数据
                    signInfoDTO.setAddress(signInfo.getAddress());
                    signInfoDTO.setId(Long.valueOf(Integer.valueOf(signInfo.getId().toString())));
                    signInfoDTO.setCompanyUserId(companyUserDTO.getId());
                    signInfoDTO.setCompanyId(CurrentUserHolder.get().getCompanyId());
                    signInfoDTO.setCreateDate(signInfo.getCreateDate());
                    signInfoDTO.setCreateMonth(signInfo.getCreateMonth());
                    signInfoDTO.setCreateTime(DateTimeUtil.dateToStr(signInfo.getCreateTime(), DateTimeUtil.STANDARD_FORMAT));
                    Date now = new Date();
                    String nowDateStr = DateTimeUtil.dateToStr(now, DateTimeUtil.TIME_FORMAT_2);
                    String dayKey = "signDay_" + signInfo.getCompanyUserId() + "_" + nowDateStr;
                    String dayCount = redisTemplate.opsForValue().get(dayKey);
                    if (StringUtils.isEmpty(dayCount)) {
                        dayCount = "0";
                    }

                    signInfoDTO.setDayCount(dayCount);
                    signInfoDTO.setLat(signInfo.getLat());
                    signInfoDTO.setLng(signInfo.getLng());
                    signInfoDTO.setImei(signInfo.getImei());
                    signInfoDTO.setRemark(signInfo.getRemark());
                    signInfoDTO.setUserName(CurrentUserHolder.get().getUsername());
                    signInfoDTO.setVisit(signInfo.getVisit());
                    //获取图片列表
                    LambdaQueryWrapper<SignPicture> queryPictureWrapper = new LambdaQueryWrapper<>();
                    queryPictureWrapper.eq(SignPicture::getSignId, signInfo.getId());
                    List<SignPicture> signPictures = signPictureMapper.selectList(queryPictureWrapper);
                    List<String> urlList = new ArrayList<>();
                    for (SignPicture signPicture : signPictures) {
                        String url = signPicture.getUrl();
                        urlList.add(url);
                    }
                    //调用方法分割集合里的url地址
                    signInfoDTO.setImages(getImages(signPictures));
                    //把获取到的图片列表传入
                    signInfoDTO.setSignPictureUrlList(urlList);
                    //把一个签到数据,封装到签到集合里面
                    signInfoDTOList.add(signInfoDTO);
                    responseListDTO.setSignResponseList(signInfoDTOList);
                    responseListDTO.setUsername(companyUserDTO.getUserName());
                }
                if (!CollectionUtils.isEmpty(signInfos)) {
                    signResponseListDTOList.add(responseListDTO);
                }
            }
            return signResponseListDTOList;
        }
        return null;
    }


    //获取图片列表字符串
    public String getImages(List<SignPicture> signPictures) {
        StringBuilder stringBuilder = new StringBuilder();
        signPictures.forEach(signPicture -> {
            stringBuilder.append(signPicture.getUrl() + ",");
        });
        return stringBuilder.toString();
    }

    @SneakyThrows
    @Override
    public List<SignInfoDTO> getMonthCount() {
        //获取当前登录的员工
        Long companyUserId = CurrentUserHolder.get().getCompanyUserId();
        //获取本月第一天和最后一天
        String firstDay = DateTimeUtil.getCurrentMonthFirstDay();
        String lastDay = DateTimeUtil.getCurrentMonthLastDay();
        //获取两个日期之间所有的日期集合
        List<String> dateList = DateTimeUtil.getSpanDateList(firstDay, lastDay);
        //遍历集合,根据每天查询签到的次数
        Integer sum = 0;


        List<SignInfoDTO> signInfoDTOList = new ArrayList<>();

        for (String date : dateList) {
            LambdaQueryWrapper<SignInfo> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(SignInfo::getCompanyUserId, companyUserId)
                    .in(SignInfo::getCreateDate, date);
            List<SignInfo> signInfos = signInfomapper.selectList(wrapper);

            if (!CollectionUtils.isEmpty(signInfos)) {
                for (SignInfo signInfo : signInfos) {
                    if (!StringUtils.isEmpty(signInfo.getRemark())) {
                        sum++;
                    }
                }

                //组装数据
                for (SignInfo signInfo : signInfos) {
                    /*
                    查询有效签到上传的图片
                     */
                    LambdaQueryWrapper<SignPicture> queryPictureWrapper = new LambdaQueryWrapper<>();
                    queryPictureWrapper.eq(SignPicture::getSignId, signInfo.getId());
                    List<SignPicture> signPictures = signPictureMapper.selectList(queryPictureWrapper);
                    List<String> urlList = new ArrayList<>();
                    for (SignPicture signPicture : signPictures) {
                        String url = signPicture.getUrl();
                        urlList.add(url);
                    }


                    SignInfoDTO signInfoDTO = new SignInfoDTO();


                    signInfoDTO.setAddress(signInfo.getAddress());
                    signInfoDTO.setCompanyId(CurrentUserHolder.get().getCompanyId());
                    signInfoDTO.setCreateDate(signInfo.getCreateDate());
                    signInfoDTO.setCreateMonth(signInfo.getCreateMonth());
                    signInfoDTO.setCreateTime(DateTimeUtil.dateToStr(signInfo.getCreateTime(), DateTimeUtil.STANDARD_FORMAT));
                    signInfoDTO.setDayCount(this.getDayCount().toString());
                    signInfoDTO.setLat(signInfo.getLng());
                    signInfoDTO.setLng(signInfo.getLat());
                    signInfoDTO.setMonthCount(sum.toString());
                    signInfoDTO.setRemark(signInfo.getRemark());
                    signInfoDTO.setImages(this.getImages(signPictures));
                    signInfoDTO.setImei(signInfo.getImei());
                    signInfoDTO.setId(Long.valueOf(signInfo.getId().toString()));
                    signInfoDTO.setSignPictureUrlList(urlList);
                    signInfoDTO.setUserName(CurrentUserHolder.get().getUsername());
                    signInfoDTO.setVisit(signInfo.getVisit());
                    signInfoDTOList.add(signInfoDTO);
                }
                return signInfoDTOList;
            }

        }
        return null;
    }


    @Override
    public PageResult<UserSignInfoDTO> sign(String startTime, String endTime, Integer page, Integer pageSize) {
        // 获取两个日期之间的日期集合
        List<String> spanDateList = DateTimeUtil.getSpanDateList(startTime, endTime);
        //首先对当月的id进行分页并去重
        IPage<SignInfo> ipage = new Page<>(page, pageSize);
        LambdaQueryWrapper<SignInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.between(SignInfo::getCreateDate, startTime, endTime);
        wrapper.eq(SignInfo::getCompanyId, CurrentUserHolder.get().getCompanyId());
        wrapper.groupBy(SignInfo::getCompanyUserId);
        wrapper.select(SignInfo::getCompanyUserId);

        ipage = signInfomapper.selectPage(ipage, wrapper);

        //获取分页数据中的员工id查询出详细信息
        List<Long> idList = ipage.getRecords().stream().map(SignInfo::getCompanyUserId).collect(Collectors.toList());
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i < idList.size(); i++) {
            if (i != idList.size() - 1) {
                stringBuilder.append(idList.get(i)).append(",");
            } else {
                stringBuilder.append(idList.get(i));
            }
        }
        String str = stringBuilder.toString();
        if (StringUtils.isEmpty(str)) {
            throw new NcException(ResponseEnum.SIGNDATA_NOT_FOUND);
        }

        List<SysCompanyUserDTO> SysCompanyUserList = sysCompanyUserFeign.queryByIdList(str).getData();
        //组装数据

        List<UserSignInfoDTO> result = SysCompanyUserList.stream().map(user -> {
            UserSignInfoDTO userSignInfoDTO = new UserSignInfoDTO();
            userSignInfoDTO.setId(user.getId());
            userSignInfoDTO.setPost(user.getPost());
            userSignInfoDTO.setUsername(user.getUserName());
            userSignInfoDTO.setWorkNumber(Long.valueOf(user.getWorkNumber()));
            SysDepartmentDTO sysDepartmentDTO = new SysDepartmentDTO();
            //部门数据
            sysDepartmentDTO.setId(user.getDepartmentId());
            sysDepartmentDTO.setName(user.getDepartmentName());
            userSignInfoDTO.setDepartment(sysDepartmentDTO);
            ArrayList<SignsDTO> signsDTOS = new ArrayList<>();
            for (String date : spanDateList) {
                SignsDTO signsDTO = new SignsDTO();
                signsDTO.setSignDate(date);
                signsDTO.setSignCount(0);
                String dayKey = "signDay_" + user.getId() + "_" + date;
                String s = redisTemplate.opsForValue().get(dayKey);
                if (!StringUtils.isEmpty(s)) {
                    signsDTO.setSignCount(Integer.valueOf(s));
                }
                signsDTOS.add(signsDTO);
            }
            userSignInfoDTO.setSigns(signsDTOS);
            return userSignInfoDTO;
        }).collect(Collectors.toList());
        return new PageResult<>(ipage.getTotal(), ipage.getPages(), result);
    }

    @Override
    public void export(String startTime, String endTime) throws IOException {
        // 查询考勤数据
        // 获取两个日期之间的日期集合
        List<String> spanDateList = DateTimeUtil.getSpanDateList(startTime, endTime);
        //首先对当月的id进行分页并去重
        LambdaQueryWrapper<SignInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.between(SignInfo::getCreateDate, startTime, endTime);
        wrapper.eq(SignInfo::getCompanyId, CurrentUserHolder.get().getCompanyId());
        wrapper.groupBy(SignInfo::getCompanyUserId);
        wrapper.select(SignInfo::getCompanyUserId);

        List<SignInfo> signInfos = signInfomapper.selectList(wrapper);

        //获取分页数据中的员工id查询出详细信息
        List<Long> idList = signInfos.stream().map(SignInfo::getCompanyUserId).collect(Collectors.toList());
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i < idList.size(); i++) {
            if (i != idList.size() - 1) {
                stringBuilder.append(idList.get(i)).append(",");
            } else {
                stringBuilder.append(idList.get(i));
            }
        }
        String str = stringBuilder.toString();
        List<SysCompanyUserDTO> SysCompanyUserList = sysCompanyUserFeign.queryByIdList(str).getData();
        //组装数据

        List<UserSignInfoDTO> result = SysCompanyUserList.stream().map(user -> {
            UserSignInfoDTO userSignInfoDTO = new UserSignInfoDTO();
            userSignInfoDTO.setId(user.getId());
            userSignInfoDTO.setPost(user.getPost());
            userSignInfoDTO.setUsername(user.getUserName());
            userSignInfoDTO.setWorkNumber(Long.valueOf(user.getWorkNumber()));
            SysDepartmentDTO sysDepartmentDTO = new SysDepartmentDTO();
            //部门数据
            sysDepartmentDTO.setId(user.getDepartmentId());
            sysDepartmentDTO.setName(user.getDepartmentName());
            userSignInfoDTO.setDepartment(sysDepartmentDTO);
            ArrayList<SignsDTO> signsDTOS = new ArrayList<>();
            for (String date : spanDateList) {
                SignsDTO signsDTO = new SignsDTO();
                signsDTO.setSignDate(date);
                signsDTO.setSignCount(0);
                String dayKey = "signDay_" + user.getId() + "_" + date;
                String s = redisTemplate.opsForValue().get(dayKey);
                if (!StringUtils.isEmpty(s)) {
                    signsDTO.setSignCount(Integer.valueOf(s));
                }
                signsDTOS.add(signsDTO);
            }
            userSignInfoDTO.setSigns(signsDTOS);
            return userSignInfoDTO;
        }).collect(Collectors.toList());

        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletResponse response = servletRequestAttributes.getResponse();
        response.setHeader("Content-disposition", "attachment;filename=Attendance.xls");
        response.setContentType("application/octet-stream;charset=UTF-8");
        response.setCharacterEncoding("utf-8");

        // 生成excel文件，下载
        EasyExcel.write(response.getOutputStream())
                .excelType(ExcelTypeEnum.XLS)
                .registerWriteHandler(this.horizontalCellStyleStrategy())
                .registerWriteHandler(new CustomHandler())
                .sheet("签到报表")
                .head(this.handleHeaders(result))
                .doWrite(this.handleData(result));
    }

    @Override
    public SignInfoDTO getById(Long id) {
        SignInfo signInfo = signInfomapper.selectById(id);
        SignInfoDTO signInfoDTO = new SignInfoDTO();
        signInfoDTO.setId(signInfo.getId());
        signInfoDTO.setAddress(signInfo.getAddress());
        signInfoDTO.setLat(signInfo.getLat());
        signInfoDTO.setLng(signInfo.getLng());
        signInfoDTO.setRemark(signInfo.getRemark());

        signInfoDTO.setImei(signInfo.getImei());
        signInfoDTO.setVisit(signInfo.getVisit());


        signInfoDTO.setCreateTime(String.valueOf(signInfo.getCreateTime()));
        signInfoDTO.setCreateMonth(signInfo.getCreateMonth());
        signInfoDTO.setCreateDate(signInfo.getCreateDate());
        signInfoDTO.setCompanyUserId(signInfo.getCompanyUserId());
        signInfoDTO.setCompanyId(signInfo.getCompanyId());

        SysCompanyUserDTO data = sysCompanyUserFeign.queryCompanyUserById(signInfo.getCompanyUserId()).getData();
        signInfoDTO.setUserName(data.getUserName());

        LambdaQueryWrapper<SignPicture> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SignPicture::getSignId, signInfoDTO.getId());
        List<SignPicture> signPictures = signPictureMapper.selectList(wrapper);
        List<String> stringList = signPictures.stream().map(SignPicture::getUrl).collect(Collectors.toList());

        signInfoDTO.setSignPictureUrlList(stringList);
        StringBuilder ss = new StringBuilder();
        for (int i = 0; i < stringList.size(); i++) {
            if (i != stringList.size() - 1) {
                ss.append(stringList.get(i)).append(",");
            } else {
                ss.append(stringList.get(i));
            }

        }

        signInfoDTO.setImages(ss.toString());
        Integer dayCount = this.getDayCount();
        signInfoDTO.setDayCount(String.valueOf(dayCount));
        Date now = new Date();
        String nowDateStr = DateTimeUtil.dateToStr(now, DateTimeUtil.TIME_FORMAT_4);
        String dayKey = "MonthDay_" + CurrentUserHolder.get().getCompanyUserId() + "_" + nowDateStr;
        //从redis中取出
        String value = redisTemplate.opsForValue().get(dayKey);
        signInfoDTO.setMonthCount(value);

        return signInfoDTO;
    }

    private List handleData(List<UserSignInfoDTO> result) {
        List<List<String>> dataList = new ArrayList<>();

        if (!CollectionUtils.isEmpty(result)) {
            // 遍历每一行数据
            for (UserSignInfoDTO signInfo : result) {
                List<String> row = new ArrayList<>();
                row.add(signInfo.getUsername());
                row.add(String.valueOf(signInfo.getWorkNumber()));
                row.add(signInfo.getDepartment().getName());
                row.add(signInfo.getPost());

                List<SignsDTO> signs = signInfo.getSigns();
                if (!CollectionUtils.isEmpty(signs)) {
                    for (SignsDTO attendsDTO : signs) {
                        row.add(String.valueOf(attendsDTO.getSignCount()));
                    }
                }
                dataList.add(row);
            }
        }
        return dataList;

    }

    private List<List<String>> handleHeaders(List<UserSignInfoDTO> signInfoDTOS) {
        List<List<String>> headers = new ArrayList<>();

        headers.add(Arrays.asList("姓名"));
        headers.add(Arrays.asList("工号"));
        headers.add(Arrays.asList("部门"));
        headers.add(Arrays.asList("职位"));

        if (!CollectionUtils.isEmpty(signInfoDTOS)) {
            UserSignInfoDTO userSignInfoDTO = signInfoDTOS.get(0);// 取出第一条数据

            List<SignsDTO> signs = userSignInfoDTO.getSigns();

            if (!CollectionUtils.isEmpty(signs)) {

                for (SignsDTO sign : signs) {
                    headers.add(Arrays.asList(sign.getSignDate()));
                }
            }

        }
        return headers;
    }


    /**
     * 设置样式
     */
    public HorizontalCellStyleStrategy horizontalCellStyleStrategy() {

        WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
        //内容策略 - 水平居中
        contentWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);

        //设置字体
        WriteFont font = new WriteFont();
        font.setFontName("阿里巴巴普惠体");
        font.setFontHeightInPoints((short) 11);
        contentWriteCellStyle.setWriteFont(font);
        HorizontalCellStyleStrategy horizontalCellStyleStrategy = new HorizontalCellStyleStrategy(contentWriteCellStyle, contentWriteCellStyle);

        return horizontalCellStyleStrategy;
    }

    @Override
    public UserInfo getSignInfoByUserIdAndCreateDate(Long userId, String createDate) {

        LambdaQueryWrapper<SignInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SignInfo::getCompanyUserId, userId);
        wrapper.eq(SignInfo::getCreateDate, createDate);
        List<SignInfo> signInfos = signInfomapper.selectList(wrapper);

        //提取出signId查询图片
        List<Long> picList = signInfos.stream().map(SignInfo::getId).collect(Collectors.toList());
        LambdaQueryWrapper<SignPicture> picWrapper = new LambdaQueryWrapper<>();
        picWrapper.in(SignPicture::getSignId, picList);
        List<SignPicture> signPictures = signPictureMapper.selectList(picWrapper);
        Map<Long, List<SignPicture>> picMap = signPictures.stream().collect(Collectors.groupingBy(SignPicture::getSignId));


        List<SignInfoDTO> signInfoDTOS = signInfos.stream().map(signInfo -> {
            SignInfoDTO signInfoDTO = new SignInfoDTO();
            signInfoDTO.setId(signInfo.getId());
            signInfoDTO.setAddress(signInfo.getAddress());
            signInfoDTO.setLat(signInfo.getLat());
            signInfoDTO.setLng(signInfo.getLng());
            signInfoDTO.setRemark(signInfo.getRemark());
            signInfoDTO.setCreateTime(DateTimeUtil.dateToStr(signInfo.getCreateTime()));
            List<SignPicture> pics = picMap.get(signInfo.getId());
            if (!CollectionUtils.isEmpty(pics)) {
                List<String> urlStringList = pics.stream().map(SignPicture::getUrl).collect(Collectors.toList());
                signInfoDTO.setSignPictureUrlList(urlStringList);
            } else {
                signInfoDTO.setSignPictureUrlList(new ArrayList<>());
            }
            return signInfoDTO;
        }).collect(Collectors.toList());

        UserInfo userInfo = new UserInfo();
        UserDTO user = new UserDTO();
        user.setIcon("https://nineclockjhc.oss-cn-guangzhou.aliyuncs.com/1638700383%281%29.jpg");
        userInfo.setSigns(signInfoDTOS);
        SysCompanyUserDTO userDTO = sysCompanyUserFeign.queryCompanyUserById(userId).getData();
        user.setUsername(userDTO.getUserName());
        SysDepartmentDTO sysDepartmentDTO = new SysDepartmentDTO();
        sysDepartmentDTO.setName(userDTO.getDepartmentName());
        userInfo.setUser(user);
        userInfo.setDepartment(sysDepartmentDTO);


        return userInfo;
    }


}
